package com.jshop.controller.admin;

import java.util.List;

import org.json.JSONException;
import com.jshop.model.Category;
public class CategoryController extends BaseController {
	public void list(){
		render("list.html");
	}
	public void treegrid(){
		List<Category> list=Category.dao.find("select * from category order by category_order asc");
		renderJson(list);
	}
	public void edit() throws JSONException{
		if(isPost()){
			Category dao=new Category();
			boolean rs=dao.set("category_name", getPara("category_name"))
			.set("category_title",getPara("category_title"))
			.set("category_id",getPara("category_id"))
			.set("category_pid",getParaToInt("category_pid",0))
			.set("category_keys", getPara("category_keys",""))
			.set("category_desc", getPara("category_desc",""))
			.set("category_order", getParaToInt("category_order")).update();
			if(rs){
				ajaxJsonSuccessMessage("修改成功");
			}else{
				ajaxJsonErrorMessage("修改失败");
			}
		}else{
			List<Category> list=Category.dao.find("select * from category order by category_order asc");
			setAttr("list", list);
			setAttr("info", Category.dao.findById(getParaToInt("id")));
			render("edit.html");
		}
	}
	public void add() throws JSONException{
		if(isPost()){
			Category dao=new Category();
			boolean rs=dao.set("category_name", getPara("category_name"))
			.set("category_title",getPara("category_title"))
			.set("category_pid",getParaToInt("category_pid",0))
			.set("category_keys", getPara("category_keys",""))
			.set("category_desc", getPara("category_desc",""))
			.set("category_order", getParaToInt("category_order")).save();
			if(rs){
				ajaxJsonSuccessMessage("添加成功");
			}else{
				ajaxJsonErrorMessage("添加失败");
			}
		}else{
			List<Category> list=Category.dao.find("select * from category order by category_order asc");
			setAttr("list", list);
			render("add.html");
		}
		
	}
	
	public void remove() throws JSONException{
		int id=getParaToInt("id");
		List<Category> list=Category.dao.find("select * from category where category_pid=?",id);
		if(list!=null&&list.size()>0){
			ajaxJsonErrorMessage("该菜单含有子菜单，不能删除");
		}else{
			boolean rs=Category.dao.deleteById(id);
			if(rs){
				ajaxJsonSuccessMessage("删除成功");
			}else{
				ajaxJsonErrorMessage("删除失败");
			}
		}
	}
}
